<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */
//declare(ticks=1);

/**
 * 聊天主逻辑
 * 主要是处理 onMessage onClose 
 */
use \GatewayWorker\Lib\Gateway;
use \GatewayWorker\Lib\think\Db;
class Events
{  
    public static $LiveChat = "";
	public static function onWorkerStart($worker)
    {
       /**
       @引入类加载文件
        */
		$path = getcwd().DIRECTORY_SEPARATOR."Applications".DIRECTORY_SEPARATOR."Chat".DIRECTORY_SEPARATOR."include_once.php";
        include_once($path);
    }
	/**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        GateWay::sendToClient($client_id,"连接成功!");
    }
   /**
    * 有消息时
    * @param int $client_id
    * @param mixed $message
    */
   public static function onMessage($client_id,$data)
   {
	   //json转数组
	   var_dump($data);
        $data = json_decode($data,true);
		//进行用户绑定
		if($data["method_type"] == "bindUidandUser"){
			self::$LiveChat->bindUidandUser($client_id,$data);
		}
		//用户发消息
		if($data["method_type"] == "sendMsgToUser"){
			self::$LiveChat->sendMsgToUser($client_id,$data);
		}
		//获取历史聊天记录
		if($data["method_type"] == "gethistroyinfo"){
			self::$LiveChat->gethistroyinfo($client_id,$data);
		}
		//用户加入客服分组
		if($data["method_type"] == "usertogroup"){
			self::$LiveChat->usertogroup($client_id,$data);
		}
		//后台客服登录后创建组
		if($data["method_type"] == "creategruop"){
			self::$LiveChat->creategruop($client_id,$data);
		}
		//客服接入用户
		if($data["method_type"] == "bussinstocutsom"){
			self::$LiveChat->bussinstocutsom($client_id,$data);
		}
		//客服获取用户等待数据
		if($data["method_type"] == "getuserlists"){
			self::$LiveChat->getuserlists($client_id,$data);
		}
		 //结束当前用户聊天接入下一位
		 if($data["method_type"] == "downnowuser"){
			self::$LiveChat->downnowuser($client_id,$data);
		}
		//删除组
		 if($data["method_type"] == "deletegroup"){
			self::$LiveChat->deletegroup($client_id,$data);
		}
		//登录接口
		if($data["method_type"] == "adminlogin"){
			self::$LiveChat->adminlogin($client_id,$data);
		}
   }
   
   /**
    * 当客户端断开连接时
    * @param integer $client_id 客户端id
    */
   public static function onClose($client_id)
   {
	   //客服退出系统
   
   }
  
}
